# -*- coding: utf-8 -*-
#!/usr/bin/python

'''
author: Bruno Alves, n.º 6152
'''
from Stack import *

'''
Class LinkedList - To create a linked list
'''
class LinkedList:
    '''
    LinkedList Constructor
    '''
    def __init__(self, N):
        self.pointers = Stack(N)
        
        for k in range(N):
            self.pointers.push(k)
            self.key =  [0 for k in range(N)]
            self.previous = [0 for k in range(N)]
            self.next = [0 for k in range(N)]
        
            self.head = None
    
    '''
    Method to create the memory pointers
    '''
    def malloc(self, k):
        x = self.pointers.pop()
        
        self.key[x] = k
        self.previous[x] = None
        self.next[x] = None
        
        return x
    
    '''
    Method to free memory space
    '''
    def free(self, x):
        self.pointers.push(x)
    
    '''
    Method to insert values on memory
    '''
    def insert(self, x):
                self.next[x] = self.head
                
                if self.head != None:
                        self.previous[self.head] = x
                        
                self.head = x
                self.previous[x] = None
    
    '''
    Method to search values on memory
    '''    
    def search(self, k):
        x = self.head
        
        while x != None and self.key[x] != k:
            x = self.next[x]
            
        return x
    
    '''
    Method to erase values from memory
    '''    
    def remove(self,x):
        if self.previous[x] != None:
            self.next[self.prev[x]] = self.next[x]
        else:
            self.head = self.next[x]
        if self.next[x] != None:
            self.previous[self.next[x]] = self.prev[x]



       
    


